#!/bin/bash -l

model=$1
experiment=$2

path_in=../OUTS/GSI/${model}/${experiment}
path_out=../OUTS/GSI/${model}/${experiment}/means
mkdir -p ${path_out}
rm ${path_out}/*

cd ${path_in}/


module load cdo
CDO_TIMESTAT_DATE=last

for year in {2000..2022}
do
	YEARbefore=$((${year}-1))
	YEARafter=$((${year}+1))
	if [ ${year} == 2000 ]; then
		cdo setyear,${YEARbefore} ${path_in}/ERA5_gsi_${year}-12.nc aux0.nc
		cdo mergetime aux0.nc ${path_in}/ERA5_gsi_${year}-01.nc ${path_in}/ERA5_gsi_${year}-02.nc aux1.nc
		rm aux0.nc
		cdo runmean,21 aux1.nc aux2.nc
		rm aux1.nc
		cdo selsmon,1 aux2.nc ${path_out}/ERA5_gsi_${year}-01.nc
		rm aux2.nc
	else
		cdo mergetime ${path_in}/ERA5_gsi_${YEARbefore}-12.nc ${path_in}/ERA5_gsi_${year}-01.nc ${path_in}/ERA5_gsi_${year}-02.nc aux1.nc
		cdo runmean,21 aux1.nc aux2.nc
		rm aux1.nc
		cdo selsmon,1 aux2.nc ${path_out}/ERA5_gsi_${year}-01.nc
		rm aux2.nc
	fi
	cdo mergetime ${path_in}/ERA5_gsi_${year}-01.nc ${path_in}/ERA5_gsi_${year}-02.nc ${path_in}/ERA5_gsi_${year}-03.nc aux1.nc
	cdo runmean,21 aux1.nc aux2.nc
	rm aux1.nc
	cdo selsmon,2 aux2.nc ${path_out}/ERA5_gsi_${year}-02.nc
	rm aux2.nc

	cdo mergetime ${path_in}/ERA5_gsi_${year}-02.nc ${path_in}/ERA5_gsi_${year}-03.nc ${path_in}/ERA5_gsi_${year}-04.nc aux1.nc
	cdo runmean,21 aux1.nc aux2.nc
	rm aux1.nc
	cdo selsmon,3 aux2.nc ${path_out}/ERA5_gsi_${year}-03.nc
	rm aux2.nc

	cdo mergetime ${path_in}/ERA5_gsi_${year}-03.nc ${path_in}/ERA5_gsi_${year}-04.nc ${path_in}/ERA5_gsi_${year}-05.nc aux1.nc
	cdo runmean,21 aux1.nc aux2.nc
	rm aux1.nc
	cdo selsmon,4 aux2.nc ${path_out}/ERA5_gsi_${year}-04.nc
	rm aux2.nc

	cdo mergetime ${path_in}/ERA5_gsi_${year}-04.nc ${path_in}/ERA5_gsi_${year}-05.nc ${path_in}/ERA5_gsi_${year}-06.nc aux1.nc
	cdo runmean,21 aux1.nc aux2.nc
	rm aux1.nc
	cdo selsmon,5 aux2.nc ${path_out}/ERA5_gsi_${year}-05.nc
	rm aux2.nc

	cdo mergetime ${path_in}/ERA5_gsi_${year}-05.nc ${path_in}/ERA5_gsi_${year}-06.nc ${path_in}/ERA5_gsi_${year}-07.nc aux1.nc
	cdo runmean,21 aux1.nc aux2.nc
	rm aux1.nc
	cdo selsmon,6 aux2.nc ${path_out}/ERA5_gsi_${year}-06.nc
	rm aux2.nc

	cdo mergetime ${path_in}/ERA5_gsi_${year}-06.nc ${path_in}/ERA5_gsi_${year}-07.nc ${path_in}/ERA5_gsi_${year}-08.nc aux1.nc
	cdo runmean,21 aux1.nc aux2.nc
	rm aux1.nc
	cdo selsmon,7 aux2.nc ${path_out}/ERA5_gsi_${year}-07.nc
	rm aux2.nc

	cdo mergetime ${path_in}/ERA5_gsi_${year}-07.nc ${path_in}/ERA5_gsi_${year}-08.nc ${path_in}/ERA5_gsi_${year}-09.nc aux1.nc
	cdo runmean,21 aux1.nc aux2.nc
	rm aux1.nc
	cdo selsmon,8 aux2.nc ${path_out}/ERA5_gsi_${year}-08.nc
	rm aux2.nc

	cdo mergetime ${path_in}/ERA5_gsi_${year}-08.nc ${path_in}/ERA5_gsi_${year}-09.nc ${path_in}/ERA5_gsi_${year}-10.nc aux1.nc
	cdo runmean,21 aux1.nc aux2.nc
	rm aux1.nc
	cdo selsmon,9 aux2.nc ${path_out}/ERA5_gsi_${year}-09.nc
	rm aux2.nc

	cdo mergetime ${path_in}/ERA5_gsi_${year}-09.nc ${path_in}/ERA5_gsi_${year}-10.nc ${path_in}/ERA5_gsi_${year}-11.nc aux1.nc
	cdo runmean,21 aux1.nc aux2.nc
	rm aux1.nc
	cdo selsmon,10 aux2.nc ${path_out}/ERA5_gsi_${year}-10.nc
	rm aux2.nc

	cdo mergetime ${path_in}/ERA5_gsi_${year}-10.nc ${path_in}/ERA5_gsi_${year}-11.nc ${path_in}/ERA5_gsi_${year}-12.nc aux1.nc
	cdo runmean,21 aux1.nc aux2.nc
	rm aux1.nc
	cdo selsmon,11 aux2.nc ${path_out}/ERA5_gsi_${year}-11.nc
	rm aux2.nc

	cdo mergetime ${path_in}/ERA5_gsi_${year}-11.nc ${path_in}/ERA5_gsi_${year}-12.nc ${path_in}/ERA5_gsi_${YEARafter}-01.nc aux1.nc
	cdo runmean,21 aux1.nc aux2.nc
	rm aux1.nc
	cdo selsmon,12 aux2.nc ${path_out}/ERA5_gsi_${year}-12.nc
	rm aux2.nc
done


